package com.xingyun.info_manager.controller;

import com.xingyun.info_manager.entity.AdminUser;
import com.xingyun.info_manager.entity.Menu;
import com.xingyun.info_manager.entity.ReturnBeans;
import com.xingyun.info_manager.service.IAdminUserService;
import com.xingyun.info_manager.service.IMenuService;
import com.xingyun.info_manager.util.JSONChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

/**
 * @Classname LoginController
 * @Description 登录
 * @Date 2020/5/16 10:39
 * @Created by You
 */
@Controller
public class LoginController {

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private IMenuService menuService;

    @ResponseBody
    @RequestMapping("/admin/login")
    public String login(@RequestParam("userName") String userName,
                        @RequestParam("userPwd") String userPwd,
                        @RequestParam("channelId") String channelId,
                      HttpSession session) throws IOException {
        //得到请求参数--模拟登录
        System.out.println("用户名密码：" + userName + " -- " + userPwd);

        //渠道id转换类型
        int chlId = Integer.parseInt(channelId);

        //返回的响应对象
        ReturnBeans returnBean = new ReturnBeans<>();

        ////查询用户名是否存在（）
        AdminUser admin = adminUserService.findByNamePwd(userName, userPwd,chlId);
        System.out.println(admin);
        //判断用户是否存在
        if(admin == null){//用户不存在,或密码错误
            returnBean.setCode(1);//错误码
            returnBean.setMsg("用户不存在或密码错误");
            //将对象转为json字符串返回
            return JSONChange.objToJson(returnBean);
        }

        //登录账号密码正确将账号存入session中
        session.setAttribute("admin", admin);
        //根据用户角色权限查询菜单
        List<Menu> parentMenuList = menuService.getParentMenuByRoleId(admin.getRoleId(),admin.getChannelId());
        List<Menu> childMenuList = menuService.getChildMenuByRoleId(admin.getRoleId(),admin.getChannelId());
        //将用户所能看到的菜单存入到session中
        session.setAttribute("parentMenuList", parentMenuList);
        session.setAttribute("childMenuList", childMenuList);
        System.out.println("--------- " + parentMenuList);
        System.out.println("--------- " + childMenuList);
        returnBean.setCode(0);//错误码
        //将对象转为json字符串返回
        return JSONChange.objToJson(returnBean);
    }

    @RequestMapping("/admin/index")
    public String index() {
        return "index";
    }

    @RequestMapping("admin/logout")
    public String logout(HttpSession session){
        Enumeration em = session.getAttributeNames();  //得到session中所有的属性名
        while (em.hasMoreElements()) {
            session.removeAttribute(em.nextElement().toString()); //遍历删除session中的值
        }
        return "redirect:/info_manager/login.html";
    }


}
